package com.blue.eagle.controller;

import java.io.IOException;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import com.blue.eagle.dao.CustomerDao;

public class AunthenticationHandler implements AuthenticationSuccessHandler {
	
	/**
	 * @uml.property  name="customerDao"
	 * @uml.associationEnd  readOnly="true"
	 */
	@Autowired
	CustomerDao customerDao ;
	
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		
		Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
		if (roles.contains("ROLE_USER")) {
			System.out.println("Yest It has RoleUser");
			response.sendRedirect("/blueEagle/resource/loginSuccess");
		}else {
			response.sendRedirect("/");
		}
		
	}
	
//	public ModelAndView singleSignOn(@ModelAttribute("loginForm") LoginForm loginForm,HttpServletRequest request) {
//		System.out.println("Single Sign ON ....");
//		ModelAndView model = new ModelAndView ("welcomePage");
//		if(loginForm.getUsername().equalsIgnoreCase("blueeagle") 
//				&& loginForm.getPassword().equalsIgnoreCase("123")) {
//			model.addObject("praise","Welcome !" );
//			
//			model.addObject("customerList", customerDao.getCustomerList());
//			model.addObject("uploadForm",new UploadForm());
//			
//		}
//		return model;
//		
//	}
	
	 
}
